import { Message } from 'antd';
import { routerRedux } from 'dva/router';
import { ROLE_TYPE_SUPER } from "../labelManager/constants";
import apis from "../services/labelManagerApi";

export default {
  namespace: "labelManager",
  state: {
    STList: [],
    totalCount: 0,
    loading: false,
    delErrCode: "-1",
    delErrMsg: "",
    departList: [],
    labelDetailInfo: {},
  },
  reducers: {
    "dump": (state, {payload}) => ({
      ...state,
      ...payload
    }),
    "get/getstList": (state, { payload }) => ({
      ...state,
      STList: payload
    }),
    "put/putloading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/puttotal": (state, { payload }) => ({
      ...state,
      totalCount: payload
    }),
    "put/putcode": (state, { payload }) => ({
      ...state,
      delErrCode: payload
    }),
    "put/putErr": (state, { payload }) => ({
      ...state,
      delErrMsg: payload
    }),
    "put/putdepart": (state, { payload }) => ({
      ...state,
      departList: payload
    })
  },
  effects: {
    *getLabelList({ payload }, { call, put }) {
      const { roleType } = payload;
      const listApi = roleType === ROLE_TYPE_SUPER ? apis.getLabelList : apis.getMyLabelList;
      yield put({
        type: "put/putloading",
        payload: true
      });
      const res = yield call(listApi, payload);
      const { datas, totalCount } = res;
      yield put({
        type: "get/getstList",
        payload: datas
      });
      yield put({
        type: "put/putloading",
        payload: false
      });
      yield put({
        type: "put/puttotal",
        payload: totalCount
      });
    },
    *saveLabel({ payload }, { call, put }) {
      const { roleType } = payload;
      const res = yield call(apis.saveLabel, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        Message.success('新增标签成功！');
        yield put(routerRedux.push({
          pathname: roleType === ROLE_TYPE_SUPER ?
            '/messageCenter/channelManager/labelManager'
            :
            '/messageCenter/channelManager/myLabelManager'
        }));
      } else {
        Message.error(errMsg || '新增标签失败！');
      }
      return errCode;
    },
    *modLabel({ payload }, { call, put }) {
      const { roleType } = payload;
      const res = yield call(apis.modLabel, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        Message.success('修改标签成功！');
        yield put(routerRedux.push({
          pathname: roleType === ROLE_TYPE_SUPER ?
            '/messageCenter/channelManager/labelManager'
            :
            '/messageCenter/channelManager/myLabelManager'
        }));
      } else {
        Message.error(errMsg || '修改标签失败！');
      }
      return errCode;
    },
    *addUser({ payload }, { call, put }) {
      const { roleType, pageSize, pageNum } = payload;
      const res = yield call(apis.modLabel, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        Message.success('添加用户成功！');
        yield put({
          type: 'getLabelList',
          payload: {
            createTime: "desc",
            labelName: "",
            status: null,
            roleType,
            pageSize,
            pageNum
          },
        });
      } else {
        Message.error(errMsg || '添加用户失败！');
      }
      return errCode;
    },
    *delLabel({ payload }, { call, put }) {
      const { roleType, pageSize, pageNum } = payload;
      const res = yield call(apis.delLabel, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        Message.success('删除标签成功！');
        yield put({
          type: 'getLabelList',
          payload: {
            createTime: "desc",
            labelName: "",
            status: null,
            roleType,
            pageSize,
            pageNum
          },
        });
      } else {
        Message.error(errMsg || '删除标签失败！');
      }
      return errCode;
    },
    *getDetail({ payload }, { call, put }) {
      const res = yield call(apis.getDetail, payload);
      const { errCode, datas } = res;
      if (errCode === 0) {
        yield put({
          type: 'dump',
          payload: { labelDetailInfo: datas },
        });
      }
      return errCode;
    },
    *updateStatus({ payload }, { call, put }) {
      const { roleType, pageSize, pageNum } = payload;
      const textTip = payload.useStatus === 1 ? '标签已启用成功！' : '标签已停用成功！';
      const textTipError = payload.useStatus === 1 ? '标签已启用失败！' : '标签已停用失败！';

      const res = yield call(apis.updateStatus, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        Message.success(textTip);
        yield put({
          type: 'getLabelList',
          payload: {
            createTime: "desc",
            labelName: "",
            status: null,
            roleType,
            pageSize,
            pageNum
          },
        });
      } else {
        Message.error(errMsg || textTipError);
      }
      return errCode;
    },
    *delSecen({ payload }, { call, put }) {
      const res = yield call(apis.delSecen, payload);
      const { errCode, errMsg } = res;
      yield put({
        type: "put/putcode",
        payload: errCode
      });
      yield put({
        type: "put/putErr",
        payload: errMsg
      });
    },
    *departData({ payload }, { call, put }) {
      const res = yield call(apis.departData, payload);
      const { datas } = res;
      yield put({
        type: "put/putdepart",
        payload: datas
      });
    }
  }
};
